package work.base.designpattern.adapter;

// 目标接口：新的媒体播放器接口
interface MediaPlayer {
    void play(String audioType, String fileName);
}

// 适配者：旧的MP3播放器
class Mp3Player {
    public void playMp3(String fileName) {
        System.out.println("播放MP3文件: " + fileName);
    }
}

// 适配者：WAV播放器（需要适配的类）
class WavPlayer {
    public void playWav(String fileName) {
        System.out.println("播放WAV文件: " + fileName);
    }
}

// 适配者：OGG播放器（需要适配的类）
class OggPlayer {
    public void playOgg(String fileName) {
        System.out.println("播放OGG文件: " + fileName);
    }
}

// 适配器：实现新接口并适配不同的音频播放器
class MediaAdapter implements MediaPlayer {
    private WavPlayer wavPlayer;
    private OggPlayer oggPlayer;

    public MediaAdapter() {
        this.wavPlayer = new WavPlayer();
        this.oggPlayer = new OggPlayer();
    }

    @Override
    public void play(String audioType, String fileName) {
        if ("wav".equalsIgnoreCase(audioType)) {
            wavPlayer.playWav(fileName);
        } else if ("ogg".equalsIgnoreCase(audioType)) {
            oggPlayer.playOgg(fileName);
        } else if ("mp3".equalsIgnoreCase(audioType)) {
            // 对于MP3，直接使用现有的播放器
            new Mp3Player().playMp3(fileName);
        } else {
            System.out.println("不支持的格式: " + audioType);
        }
    }
}

// 客户端：使用新的媒体播放器接口
public class AdapterPatternDemo {
    public static void main(String[] args) {
        MediaPlayer player = new MediaAdapter();

        player.play("mp3", "song.mp3");
        player.play("wav", "sound.wav");
        player.play("ogg", "music.ogg");
        player.play("flac", "file.flac");
    }
}

